/*  
 *  ------ [Ev_3] - Socket 3 Reading for Gases v20-------- 
 *  
 *  Explanation: Turn on the Events Board v20 configuring the threshold
 *  of the socket 3 stage and wait for an interruption from this sensor
 *  to read its value and print the resistance of the sensor
 *  
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    
 */

#include <WaspSensorEvent_v20.h>

#define THRESHOLD 1.0  // THRESHOLD for interruption from the sensor

// Variable to store the read value
float value;

void setup()
{
  // 1. Initialization of the modules  
  
  // Turn on the USB and print a start message
  USB.ON();
  USB.println(F("start"));
  delay(100);

  // Turn on the sensor board
  SensorEventv20.ON();

  // Turn on the RTC
  RTC.ON();

  // Configure the socket 3 threshold
  SensorEventv20.setThreshold(SENS_SOCKET3, THRESHOLD);
  
  // Enable interruptions from the board
  SensorEventv20.attachInt();
  
}
 
void loop()
{
  // 2. In case of being interrupted by the sensor board
  if(intFlag & SENS_INT)
  {
    // Disable interruptions from the board
    SensorEventv20.detachInt();
    
    // Load the interruption flag
    SensorEventv20.loadInt();
    
    // Read the sensor
    value = SensorEventv20.readValue(SENS_SOCKET3, SENS_RESISTIVE);
    
    // 3. In case the interruption came from socket 3
    if( SensorEventv20.intFlag & SENS_SOCKET3)
    {
      USB.println(F("Interruption from socket 3"));
    }

    // 4. Printing and enabling interruptions

    // Print the resistance of the sensor
    USB.print(F("Resistance: "));    
    USB.print(value);
    USB.println(F("kohms"));
    
    // Clean the interruption flag
    clearIntFlag();
    
    // Enable interruptions from the board
    SensorEventv20.attachInt();
  }
}
